package DataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

import pojo.NodeThaoLuanPOJO;

public class NodeThaoLuanDA extends BaseDA {

	@Override
	protected NodeThaoLuanPOJO getProperties(ResultSet rss) throws SQLException {
		NodeThaoLuanPOJO rs = new NodeThaoLuanPOJO();
		rs.setGhiChu(rss.getString("GhiChu"));
		rs.setId(rss.getInt("ID"));
		rs.setIdCay(rss.getInt("IDCay"));
		rs.setNguoiTao(rss.getString("NguoiTao"));
		rs.setIdNodeCha(rss.getInt("IDNodeCha"));
		rs.setNoiDung(rss.getString("NoiDung"));
		rs.setVersion(rss.getTimestamp("version").getTime());
		return rs;
	}

	public NodeThaoLuanPOJO get(int id) {
		String sql = "select * from nodethaoluan where id=" + id;
		NodeThaoLuanPOJO rs = null;
		try {
			ResultSet rss = provider.executeQuery(sql);
			if (rss.next()) {
				rs = getProperties(rss);
			}
		} catch (Exception e) {
			rs = null;
		}
		return rs;
	}

	public ArrayList<NodeThaoLuanPOJO> getByIDCay(int idCay) {
		String sql = "select * from nodethaoluan where IDCay=" + idCay;
		ArrayList<NodeThaoLuanPOJO> rs = null;
		try {
			rs = new ArrayList<NodeThaoLuanPOJO>();
			ResultSet rss = provider.executeQuery(sql);
			while (rss.next()) {
				NodeThaoLuanPOJO node = getProperties(rss);
				rs.add(node);
			}
		} catch (Exception e) {
			rs = null;
		}
		return rs;
	}

	public int insertNode(NodeThaoLuanPOJO node) {
		int rs=-1;
		String procName="spInsertNodeThaoLuan";
		String []input=new String[]{node.getIdNodeCha()+"",node.getIdCay()+"",node.getNguoiTao()+"",node.getNoiDung(),node.getGhiChu()};
		try {
			ResultSet rss = provider.executeStore(procName, input);
			if (rss.next()) {
				rs= rss.getInt("ID");
				NodeThaoLuanPOJO n=get(rs);
				node.setId(n.getId());
				node.setVersion(n.getVersion());
				node.setTinhTrang(n.getTinhTrang());
				//Update version cây
				new CayThaoLuanDA().updateVersion(n.getIdCay(), n.getVersion());
			}
		} catch (Exception e) {
			rs=-1;
		}
		return rs;
	}
	public long deleteNode(int idCay,int idNode){
		long rs=-1;
		String sql="update nodethaoluan set TinhTrang=1, version=now() where id="+idNode+"  and idcay="+idCay;
		provider.executeUpdate(sql);
		System.out.println("Da xoa node id="+idNode);
		ArrayList<NodeThaoLuanPOJO> lstNodeCon=getNodeCon(idNode);
		if(lstNodeCon!=null){
			for(NodeThaoLuanPOJO n:lstNodeCon){
				deleteNode(idCay, n.getId());
			}
		}
		rs=get(idNode).getVersion();
		//Update version cây
		new CayThaoLuanDA().updateVersion(idCay, rs);
		return rs;
	}
	public long update(NodeThaoLuanPOJO node){
		String sql="update nodethaoluan set version=now(), NoiDung='"+node.getNoiDung()+"', GhiChu='"+node.getGhiChu()+"' where id="+node.getId()+" and idcay="+node.getIdCay();
		provider.executeUpdate(sql);
		long rs=get(node.getId()).getVersion();
		node.setVersion(rs);
		new CayThaoLuanDA().updateVersion(node.getId(), rs);
		return rs;
	}
	public ArrayList<NodeThaoLuanPOJO> getNodeCon(int idCha){
		String sql = "select * from nodethaoluan where IDNodeCha=" + idCha;
		ArrayList<NodeThaoLuanPOJO> rs = null;
		try {
			rs = new ArrayList<NodeThaoLuanPOJO>();
			ResultSet rss = provider.executeQuery(sql);
			while (rss.next()) {
				NodeThaoLuanPOJO node = getProperties(rss);
				rs.add(node);
			}
		} catch (Exception e) {
			rs = null;
		}
		return rs;
	}
}
